package homework;

import java.util.Stack;

/**
 * 使用2个栈实现队列
 * @author noob
 * @version 1.0
 * @date 2021/3/15 7:35
 */
public class MyQueue3 {
    private Stack<Integer> stack1;
    private Stack<Integer> stack2;
    int front;

    public MyQueue3() {
        stack1 = new Stack<>();
        stack2 = new Stack<>();
    }

    /**
     * 入队
     * @param x
     */
    public void  push(int x){
        if(stack1.isEmpty()){
            front = x;
        }
        stack1.push(x);
    }

    /**
     * 出队操作
     * @return
     */
    public int pop() {

        // 如果 stack2 不为空，直接返回 stack2 的栈首元素
        if(!stack2.isEmpty()) {
            return stack2.pop();
        }

        while(stack1.size() > 1) {
            stack2.push(stack1.pop());
        }

        return stack1.pop();
    }

    public int peek() {
        if(!stack2.isEmpty()) {
            return stack2.peek();
        }
        return front;
    }


    public boolean empty() {
        return stack1.isEmpty() && stack2.isEmpty();
    }

}
